package com.aaa.Service.Impl;

import com.aaa.Dao.ProductDao;
import com.aaa.Entity.Product;
import com.aaa.Service.ProductService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;


    public TableData<Product> listAll(Integer page, Integer limit) {
        TableData<Product> tableData = new TableData<Product>();
        //第一步，查询当前页的数据
        Integer offset = (page-1)*limit;
        List<Product> data = productDao.listAll(offset, limit);
        tableData.setData(data);
        //第二步，查询总条数
        Integer count = productDao.count();
        tableData.setCount(count);
        return tableData;
    }

    public DefaultMsg Delete(Integer id) {
        DefaultMsg msg= new DefaultMsg();
        Integer delete = productDao.Delete(id);
        if(delete>0){
            return msg;
        }else {
            msg.setError("操作失败");
            return msg;
        }
    }

    public DefaultMsg AddOrUpdate(Product product) {
        Integer count;
        if(product.getId()!=null){
            count =productDao.Update(product);
        }else {
            count =productDao.Add(product);
        }
        DefaultMsg msg = new DefaultMsg();
        if (count>0){
            return msg;
        }else {
            msg.setError("操作失败");
            return msg;
        }
    }
}
